package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.product;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSONObject;
import java.net.URLEncoder;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.PlanDetailBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.StatisticPlanBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.product.PdtPlanBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.PdtPlanDetailExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.PdtPlanExcelEn;
import com.xiaoxu.intranetweb.util.PermissionCheck;
import com.xiaoxu.intranetweb.util.SzResult;
import com.xiaoxu.intranetweb.util.WebFunctionUtil;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

@RestController
@RequestMapping("/plan")
public class PlanController {
    @Autowired
    private PdtPlanBu pdtPlanBu;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private WebFunctionUtil webFunctionUtil;
//    查询总PdtPlan

    /**
     * 查询总PdtPlan
     *
     * @param params
     * @return
     */
    @PostMapping("/getAllPdtPlan")
    @PermissionCheck("plan_getAllPdtPlan")
    public SzResult getAllPdtPlan(@RequestBody StatisticPlanBo params, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return pdtPlanBu.getAllPdtPlan(params, StpUtil.getLoginIdByToken(token).toString(), plantId);
    }

    //    删除发货计划
    @GetMapping("/deletePdtPlan")
    @PermissionCheck("plan_deleteAllPdtPlan")
    public SzResult deletePdtPlan(@RequestParam("pdtPlanexId") String pdtPlanexId, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        return pdtPlanBu.deletePdtPlan(pdtPlanexId, token);
    }
//    查询发货计划详情

    /**
     * 查询发货计划详情
     *
     * @param params
     * @return
     */
    @PostMapping("/getPdtPlanDetail")
    @PermissionCheck("plan_getPdtPlanDetail")
    public SzResult getPdtPlanDetail(@RequestBody PlanDetailBo params, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return pdtPlanBu.getPdtPlanDetail(params, StpUtil.getLoginIdByToken(token).toString(), plantId);
    }
//    新增发货计划

    /**
     * 新增发货计划
     *
     * @param params
     * @return
     */
    @PostMapping("/addPdtPlanDetail")
    @PermissionCheck("addPdtPlanDetail")
    public SzResult addPdtPlanDetail(@RequestBody PlanDetailBo params, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return pdtPlanBu.addPdtPlanDetail(params, StpUtil.getLoginIdByToken(token).toString(), plantId);
    }

    //    修改发货计划
    @PostMapping("/updatePdtPlanDetail")
    @PermissionCheck("updatePdtPlanDetail")
    public SzResult updatePdtPlanDetail(@RequestBody PlanDetailBo params, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return pdtPlanBu.updatePdtPlanDetail(params, StpUtil.getLoginIdByToken(token).toString(), plantId);
    }


    /**
     * 导表
     */

    @PostMapping("/pdtPlanExportExcel")
    public void pdtPlanExportExcel(@RequestBody JSONObject jsonParam, HttpServletResponse response, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
//        String plantId = null;
//        if (token !=null) {
//            plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
//        }
        //        实体类
        List<Map<String, Object>> fields = jsonParam.getObject("fields", List.class);
//        文件名使用utf-8编码
        String filename = jsonParam.getString("filename");
//        数据
        List<Map<String, Object>> ids = jsonParam.getObject("ids", List.class);
//        是否有表头
        Boolean isHeader = jsonParam.getBoolean("isHeader");
//        mode为"current"时，导出当前页，为"all"时，导出所有页，selected时，导出选中数据
        String mode = jsonParam.getString("mode");
//        是否原始数据
        Boolean original = jsonParam.getBoolean("original");
//        分页
        Map<String, String> pager = jsonParam.getObject("pager", Map.class);
//        表格标题
        String sheetName = jsonParam.getString("sheetName");
        Map<String, String> form = jsonParam.getObject("form", Map.class);
        StatisticPlanBo statisticPlanBo = new StatisticPlanBo();
        if (form != null) {
            if (form.get("minDate") != null) {
                statisticPlanBo.setMinDate(form.get("minDate"));
            }
            if (form.get("maxDate") != null) {
                statisticPlanBo.setMaxDate(form.get("maxDate"));
            }
            if (form.get("coName") != null) {
                statisticPlanBo.setCoName(form.get("coName"));
            }
            if (form.get("coId") != null) {
                statisticPlanBo.setCoId(form.get("coId"));
            }
            if (form.get("planType") != null) {
                statisticPlanBo.setPlanType(form.get("planType"));
            }

            statisticPlanBo.setDataFrom(plantId);

            if (form.get("pdtId") != null) {
                statisticPlanBo.setPdtId(form.get("pdtId"));
            }
        }
        Future<List<PdtPlanExcelEn>> listFuture = pdtPlanBu.pdtPlanExportExcel(fields, ids, filename, isHeader, mode, original, pager, form, sheetName, response, statisticPlanBo);
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        BorderStyle borderStyle = BorderStyle.THIN;
        short borderColor = IndexedColors.BLACK.getIndex();
        contentWriteCellStyle.setBorderTop(borderStyle);
        contentWriteCellStyle.setBorderBottom(borderStyle);
        contentWriteCellStyle.setBorderLeft(borderStyle);
        contentWriteCellStyle.setBorderRight(borderStyle);
        contentWriteCellStyle.setTopBorderColor(borderColor);
        contentWriteCellStyle.setBottomBorderColor(borderColor);
        contentWriteCellStyle.setLeftBorderColor(borderColor);
        contentWriteCellStyle.setRightBorderColor(borderColor);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
        List<PdtPlanExcelEn> pdtPlanExcelEn = listFuture.get();
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        if (filename == null || filename.equals("")) {
            filename = "品种发货计划";
        }
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PdtPlanExcelEn.class).registerWriteHandler(horizontalCellStyleStrategy).sheet("品种发货计划").doWrite(pdtPlanExcelEn);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }


    /**
     * 发货计划详情导表
     */

    @PostMapping("/pdtPlanDetailExportExcel")
    public void pdtPlanDetailExportExcel(@RequestBody JSONObject jsonParam, HttpServletResponse response, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
//        String plantId = null;
//        if (token !=null) {
//            plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
//        }
        //        实体类
        List<Map<String, Object>> fields = jsonParam.getObject("fields", List.class);
//        文件名使用utf-8编码
        String filename = jsonParam.getString("filename");
//        数据
        List<Map<String, Object>> ids = jsonParam.getObject("ids", List.class);
//        是否有表头
        Boolean isHeader = jsonParam.getBoolean("isHeader");
//        mode为"current"时，导出当前页，为"all"时，导出所有页，selected时，导出选中数据
        String mode = jsonParam.getString("mode");
//        是否原始数据
        Boolean original = jsonParam.getBoolean("original");
//        分页
        Map<String, String> pager = jsonParam.getObject("pager", Map.class);
//        表格标题
        String sheetName = jsonParam.getString("sheetName");
        Map<String, String> form = jsonParam.getObject("form", Map.class);
        PlanDetailBo planDetailBo = new PlanDetailBo();
        if (form != null) {
            if (form.get("minDate") != null) {
                planDetailBo.setMinDate(form.get("minDate"));
            }
            if (form.get("maxDate") != null) {
                planDetailBo.setMaxDate(form.get("maxDate"));
            }
            if (form.get("coName") != null) {
                planDetailBo.setCoName(form.get("coName"));
            }
            if (form.get("coId") != null) {
                planDetailBo.setCoId(form.get("coId"));
            }
            if (form.get("planType") != null) {
                planDetailBo.setPlanType(form.get("planType"));
            }

            planDetailBo.setDataFrom(plantId);

            if (form.get("pdtId") != null) {
                planDetailBo.setPdtId(form.get("pdtId"));
            }
        }
        Future<List<PdtPlanDetailExcelEn>> listFuture = pdtPlanBu.pdtPlanDetailExportExcel(fields, ids, filename, isHeader, mode, original, pager, form, sheetName, response, planDetailBo);
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        BorderStyle borderStyle = BorderStyle.THIN;
        short borderColor = IndexedColors.BLACK.getIndex();
        contentWriteCellStyle.setBorderTop(borderStyle);
        contentWriteCellStyle.setBorderBottom(borderStyle);
        contentWriteCellStyle.setBorderLeft(borderStyle);
        contentWriteCellStyle.setBorderRight(borderStyle);
        contentWriteCellStyle.setTopBorderColor(borderColor);
        contentWriteCellStyle.setBottomBorderColor(borderColor);
        contentWriteCellStyle.setLeftBorderColor(borderColor);
        contentWriteCellStyle.setRightBorderColor(borderColor);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
        List<PdtPlanDetailExcelEn> pdtPlanDetailExcelEn = listFuture.get();
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        if (filename == null || filename.equals("")) {
            filename = "品种发货计划详情";
        }
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PdtPlanDetailExcelEn.class).registerWriteHandler(horizontalCellStyleStrategy).sheet("品种发货计划详情").doWrite(pdtPlanDetailExcelEn);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }
}
